<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">智能云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu mode="horizontal" :selected-keys="selectedKey" @click="handleMenuClick">
          <a-menu-item class="menuItem" v-for="item in visibleRoutes" :key="item.path"
            >{{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { LogoutOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const loginUserStore = useLoginUserStore()

import checkAccess from '@/access/checkAccess.ts'
import { routes } from '@/router/routes.ts'

const router = useRouter()
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    console.log('item', item)
    console.log('userRole', loginUserStore.loginUser.userRole)
    if (item.meta?.hideinmenu) {
      return false
    }
    //根据权限过滤菜单
    console.log('loginuser', loginUserStore.loginUser)
    if (!checkAccess(loginUserStore.loginUser, item.meta?.access as any)) {
      return false
    }
    return true
  })
})
// 当前选中的菜单, 默认主页
  const selectedKey = ref(['/'])

// 监听路由变化
  router.afterEach((to, from, failure) => {
    selectedKey.value = [to.path]
  })

//路由跳转事件
  const handleMenuClick = ({ key }: any) => {
    router.push({
      path: key
    })
  }


// 用户注销
  const doLogout = async () => {
    const res = await userLogoutUsingPost()
    if (res.data.code === 200) {
      // loginUserStore.setLoginUser({
      //   userName: '未登录',
      // })
      loginUserStore.clearState()
      message.success('退出登录成功')
      await router.push('/user/login')
    } else {
      message.error('退出登录失败，' + res.data.message)
    }
  }
</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
